/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.bancofloripa.controller;

import br.com.bancofloripa.dao.AgenciaDAO;
import br.com.bancofloripa.entity.Agencia;
import br.com.bancofloripa.exception.MensagemExceptionDAO;
import br.com.bancofloripa.view.AgenciaUi;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author fabianoc
 */
public class AgenciaController {
    
    private int valorClick;
    private int id;
    Agencia ag;
    AgenciaDAO agD;    
    
    public void getClique(java.awt.event.MouseEvent evt, JTable jTable2, JTextField jtn, JTextField jti) {
        if ( evt.getClickCount() >= 1 ){
            this.setValorClick((int) (Integer) jTable2.getValueAt( jTable2.getSelectedRow() , 0));
                ag = new AgenciaDAO().getByFilter(getValorClick());
                if (ag != null) {
                    jtn.setText(ag.getNome());
                    jti.setText(String.valueOf(ag.getId()));
                    System.out.println("id "+ag.getId()+ag.getNome());
                }
        }  
    }
    
   public void atualizaTabela(List<Agencia> lista, JTable tb ){

        DefaultTableModel model = (DefaultTableModel)tb.getModel();
        model.setRowCount( lista.size() );
        for ( int i = 0; i < lista.size(); i++ ){
            model.setValueAt( lista.get(i).getId() , i, 0);
            model.setValueAt( lista.get(i).getNome() , i, 1);
         }
    }
   
   public List<Agencia> montandoList() {
       
       List<Agencia> lista = new AgenciaDAO().getAll();
    
       return lista;
       
   }
   
   public void salvar(JTextField tfId, JTextField tfNome, JTable jTable1, String tipo){
         try {
            if (tfNome.getText() != null) {
                agD = new AgenciaDAO();
                if (tipo.equalsIgnoreCase("atualiza")){
                    int cod = Integer.parseInt(tfId.getText());
                    ag = new Agencia(cod, tfNome.getText());
                    agD.atualiza(ag);
                    System.out.println("valor click"+valorClick);
                } else {
                    ag = new Agencia(tfNome.getText());
                    agD.insere(ag);
                }    
            }
            tfNome.setText("");
            tfId.setText("");
            atualizaTabela(montandoList(), jTable1);            
        } catch (MensagemExceptionDAO ex) {
            Logger.getLogger(AgenciaUi.class.getName()).log(Level.SEVERE, null, ex);
        }       
   }

    /**
     * @return the valorClick
     */
    public int getValorClick() {
        return valorClick;
    }

    /**
     * @param valorClick the valorClick to set
     */
    public void setValorClick(int valorClick) {
        this.valorClick = valorClick;
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }
    
}
